// 显示用户信息模块

// 引入User集合规则
const User = require('../../model/user.js');
// 引入mongoose-sex-page分页
const pagination = require('mongoose-sex-page');

module.exports = async (req, res, next) => {
    let { page } = req.query;
    let limit = 5;
    try {
        // 查询最大页码
        let temp = await pagination(User).page(1).size(limit).display(5).exec();
        let maxPage = temp.pages;
        // 页码判定
        if (page && page > maxPage) page = maxPage;
        else if (page && page > 1 && page <= maxPage);
        else page = 1;
        // 查询当前页码下的数据
        let result = await pagination(User).page(page).size(limit).display(5).exec();
        // 渲染用户管理页面
        req.app.locals.currentLink = 'user';
        res.render('admin/user', { data: result });
    } catch (error) {
        next(error);
    }
}